import country from "country/country"
import util from "../../utils/util.js"

const app = getApp()
Page({

    data: {
        helper: {

            countryArray: country,
            buy_wayArray: ["实体店购买", "网上购买"], //购买方式
            buy_wayIndex: 0,
            pay_wayArray: ["微信支付", "支付宝支付"],
            pay_wayIndex: 0,
        },
        form: {
            goods_number: "1",
            buy_way: "实体店购买",
            pay_way: "微信支付",
            end_date: util.formatTime(new Date(), "YYYY-MM-DD"),
            buyer_provinces: "重庆市,重庆市,南岸区"
        },

    },

    onLoad: function (options) {

    },
    async fillUserInfo() {
        const { form } = this.data
        const res = await app.request({ url: 'getUser', data: { openid: app.openid }, method: "POST" })
        if (res.status >= 300) return this.showModal({ content: "获取失败" })
        const user = res.data[0]
        form.buyer_address = user.address
        form.buyer_name = user.name
        form.buyer_phone = user.phone
        form.buyer_provinces = user.provinces
        this.setData({
            form
        })
    },
    onInput(e) {
        const { key } = e.target.dataset
        const { value } = e.detail
        const { form } = this.data
        form[key] = value
        this.setData({
            form
        })
    },
    bindPickerChange(e) {
        console.log(e);
        const { key, array } = e.target.dataset
        const { value: index } = e.detail
        const { form, helper } = this.data
        const keyArrayName = key + 'Array'
        const keyIndexName = key + 'Index'
        const value = helper[keyArrayName][index]
        form[key] = value
        helper[keyIndexName] = index
        this.setData({
            form,
            helper
        })
    },
    bindMultiPickerChange(e) {
        console.log(e);
        const { key, array } = e.target.dataset
        let { value } = e.detail
        const { form, helper } = this.data
        value = value.toString()
        form[key] = value
        helper[key] = value
        this.setData({
            form,
            helper
        })
    },
    onClickTipsPrice() {
        this.showModal({ content: '应付金额=单价*数量+赏金' })
    },
    showModal(options) {
        wx.showModal({
            title: '提示',
            content: '内容',
            showCancel: false,
            confirmText: '知道了',
            confirmColor: '#1B819E',
            ...options
        });
    },
    async onSubmit() {
        const { form } = this.data
        if (!form.end_date || form.end_date.trim() === '') return this.showModal({ content: '请完善 截止日期' })
        if (!form.bounty || form.bounty.trim() === '') return this.showModal({ content: '请完善 赏金' })
        if (!form.goods_name || form.goods_name.trim() === '') return this.showModal({ content: '请完善 商品名称' })
        if (!form.single_price || form.single_price.trim() === '') return this.showModal({ content: '请完善 商品单价' })
        if (!form.goods_number || form.goods_number.trim() === '') return this.showModal({ content: '请完善 商品数量' })
        if (!form.goods_type || form.goods_type.trim() === '') return this.showModal({ content: '请完善 商品型号' })
        if (!form.buy_way || form.buy_way.trim() === '') return this.showModal({ content: '请完善 购买方式' })
        // if(!form.buy_way_instructions||form.buy_way_instructions.trim()==='')return this.showModal({content:'请完善 购买方式说明'})
        if (!form.goods_region || form.goods_region.trim() === '') return this.showModal({ content: '请完善 商品地区' })
        if (!form.buyer_name || form.buyer_name.trim() === '') return this.showModal({ content: '请完善 收货人姓名' })
        if (!form.buyer_phone || form.buyer_phone.trim() === '') return this.showModal({ content: '请完善 收货人电话' })
        if (!form.buyer_provinces || form.buyer_provinces.trim() === '') return this.showModal({ content: '请完善 收货地区' })
        if (!form.buyer_address || form.buyer_address.trim() === '') return this.showModal({ content: '请完善 详细地址' })
        if (!form.pay_way || form.pay_way.trim() === '') return this.showModal({ content: '请完善 支付信息' })
        form.total_price = parseFloat(form.bounty) + parseFloat(form.single_price) * parseFloat(form.goods_number)
        form.buyer_openid = app.openid
        if (!form.buyer_openid || form.buyer_openid.trim() === '') return this.showModal({ content: '无法获取buyer_openid，请重新登录' })
        this.setData({ form })
        if (isNaN(form.total_price)) return this.showModal({
            content: `错误！无法计算应付金额，请您输入正确的费用，
            \r\n应付金额=单价*数量+赏金` })
        if (form.pay_way === "微信支付") form.order_id = `wx${app.openid}${new Date().getTime()}`
        if (form.pay_way === "支付宝支付") form.order_id = `zfb${app.openid}${new Date().getTime()}`
        // form.order_id = `${form.buyer_name}${form.buyer_phone}`
        const modal = await new Promise((resolve, reject) => {
            wx.showModal({
                title: '<<免责声明>>',
                content: `重邮易海淘提醒您：在使用重邮易海淘（以下简称易海淘）支付之前，请您务必仔细阅读并理解透彻本声明。您可以选择不使用易海淘，但如果您使用易海淘，您的使用行为将被视为对本声明全部内容的认可。
                \r\n订单支付使用张贴微信收款码图片的方式完成个人收款。支付账户为易海淘的管理者，管理者核对订单后，方可。除易海淘张贴的服务收款外，其他一切因使用易海淘而可能遭致的意外、疏忽、侵权及其造成的损失（包括因链接到的第三方网站内容非法支付)，易海淘对其概不负责
                `,
                cancelText: '我再想想',
                cancelColor: '#000000',
                confirmText: '同意下单',
                confirmColor: '#1B819E',
                success: async (result) => {
                    resolve(result)
                },
            });
        })
        if (modal.cancel) return
        const res = await app.request({ url: "addOrder", data: form, method: "POST" })
        if (res.meta.status >= 300) return this.showModal({ content: '创建订单失败' + res.meta.msg })
        this.setData({
            form: {
                goods_number: 1,
                buy_way: "实体店购买",
                pay_way: "微信支付",
                end_date: util.formatTime(new Date(), "YYYY-MM-DD"),
                buyer_provinces: "重庆市,重庆市,南岸区"
            },
        })
        form.id=res.data.id
        wx.navigateTo({ url: `/pages/order/orderDetail/orderDetail?id=${form.id}`+form, })
    },
    fillForm() {
        let { form } = this.data
        form = {
            ...form,
            // buy_way: "实体店购买",
            // pay_way: "微信支付",
            // end_date: "2021-04-15",
            buyer_provinces: "重庆市,重庆市,南岸区",
            bounty: "10",
            single_price: "210",
            goods_name: "Audio Technica/铁三角 ATH0CK350IS 手机通用线控带麦入耳式耳机",
            goods_type: "金色",
            goods_region: "日本",
            goods_number: "3",
            buy_way_instructions: "",
            buyer_name: "邹梦婕",
            buyer_phone: "15487425748",
            buyer_address: "重庆邮电大学30栋721",
            note: "铁三角还是没让我失望过了，之前是没有这款买的另外一款，用了快一年了换这款，音质还是不错的，麦也OK，录入的音质很棒，还送了个收纳盒，夏天不想带包，可以放钥匙在这了，一如既往的让人满意，下次有线耳机还会回购，感谢客服大白的耐心解答哦",
        }
        this.setData({ form })
    },

})